#############################################################################
# fig1: Percentage of non-partisan voters in the five countries under study #
#############################################################################


library(ggplot2)
library(tidyverse)
library(zoo)
library(gridExtra)





###-----usa
usa<-read.csv("USA_FINAL.csv")
attach(usa)

usa_df<-data.frame(usa$independent,usa$yearMon)
names(usa_df)[2]<-"yearMon"

###------uk
uk<-read.csv("UK_FINAL.csv")
attach(uk)

uk_df<-data.frame(uk$perindependent,uk$yearMon)
names(uk_df)[2]<-"yearMon"

###----germany
german<-read.csv("GERMAN_FINAL.csv")
attach(german)

#calculating the sum of percentage and the percentage of the independent
german$allp_gr=.n+ADM_ALL+AFD_AL+ALFA+andere.P+BP.Bay+BundFre+Bundnis+Burgerpa+BuSo.Bu+
  CDU+CM_Chri+DA+DasNeue+DBD+DieViol+DKP+DSU+DVU+Familie+Feminist+FWD_Fr+
  GAZ+GLU+Graue.Pa+Grune+MLPD_M+NPD+ODP+ODPD+PBC_Pa+PDS.die+Piratenp+PSG_Pa+
  Rentnerp+Republik+RRP...Re+Schill.P+SPD+Statt.Pa+Tierschu+WASG_w+Zentrum
german$indep_gr=1-german$allp_gr

ger_df<-data.frame(german$indep_gr,german$yearMon)
names(ger_df)[2]<-"yearMon"


###-----Japan
#load data
jp<-read.csv("JAPAN_FINAL.csv")
attach(jp)

jpn_df<-data.frame(jp$indep,jp$yearMon)
names(jpn_df)[2]<-"yearMon"


###----denmark
denmark<-read.csv("DENMARK_FINAL.csv")
attach(denmark)
denmark$indep_dnk=100-denmark$allp_dnk

dnk_df<-data.frame(denmark$indep_dnk,denmark$yearMon)
names(dnk_df)[2]<-"yearMon"


###-----unifying data
data1<-left_join(dnk_df,ger_df,by="yearMon")
data2<-left_join(data1,jpn_df,by="yearMon")
data3<-left_join(data2,usa_df,by="yearMon")
data_unified<-left_join(data3,uk_df,by="yearMon")
names(data_unified)[1]<-"Denmark"
names(data_unified)[3]<-"Germany"
names(data_unified)[4]<-"Japan"
names(data_unified)[5]<-"USA"
names(data_unified)[6]<-"UK"
data_unified$Denmark[data_unified$Denmark==100]<-NA
data_unified$Germany<-data_unified$Germany*100


###------plot

data_indep<-data_unified %>%
  gather(key="variable", value="value", -yearMon)

fig1<-ggplot(data_indep, aes(x = as.yearmon(yearMon), y = value)) + 
  geom_line(aes(color = variable), size = 1) +
  theme_minimal()+
  xlab("Year")+
  ylab("Percentage of Inependent Voters")

#export
windows(width = 20, height = 16)
ggsave(file = "fig1.jpeg", plot = fig1)



